package com.kyuketsukistudios.mikatsuki.preloader
{
	import caurina.transitions.Tweener;
	import com.kyuketsukistudios.mikatsuki.assetmanager.AssetManager;
	import com.kyuketsukistudios.mikatsuki.data.SingletonData;
	import com.kyuketsukistudios.mikatsuki.Engine;
	import flash.display.Sprite;
	import flash.display.Stage;
	import flash.events.Event;
	import org.casalib.display.CasaSprite;
	/**
	 * ...
	 * @author RZEROSTERN
	 */
	public class Advices extends CasaSprite
	{
		private var m_assetManager:AssetManager;
		private var m_stageReference:Stage;
		private var m_adviceThematic:Sprite;
		private var m_adviceAutoSave:Sprite;
		
		public function Advices() 
		{
			SingletonData.getInstance().sendToConsole(0xffffff, "Loaded Advices");
			m_assetManager = new AssetManager(onLibraryLoaded, "preloadAssets");
		}
		
		private function onLibraryLoaded():void 
		{
			this.m_stageReference = Engine.m_stageReference;
			m_adviceThematic = m_assetManager.getMyAsset("InMemoriamText", "spr");
			m_adviceThematic.name = "AdvertenciaInMemoriam";
			
			m_adviceAutoSave = m_assetManager.getMyAsset("WarningSaveGame", "spr");
			m_adviceAutoSave.name = "AdvertenciaAutoGuardado";
			
			m_adviceAutoSave.x = m_adviceThematic.x = m_stageReference.stageWidth / 2;
			m_adviceAutoSave.y = m_adviceThematic.y = m_stageReference.stageHeight / 2;
			m_adviceAutoSave.alpha = m_adviceThematic.alpha = 0;
			m_adviceThematic.addEventListener(Event.ENTER_FRAME, onAdviceEnterFrame);
			
			m_stageReference.addChildAt(m_adviceThematic, 0);
			m_stageReference.addChildAt(m_adviceAutoSave, 0);
		}
		
		private function onAdviceEnterFrame(p_event:Event):void 
		{
			p_event.currentTarget.removeEventListener(Event.ENTER_FRAME, onAdviceEnterFrame);
			Tweener.addTween(m_adviceThematic, { alpha: 1, time: 2 });
			Tweener.addTween(m_adviceThematic, { alpha: 0, time: 2, delay: 5, onComplete:removeSprThematic } );
		}
		
		private function removeSprThematic():void 
		{
			m_stageReference.removeChild(m_adviceThematic);
			SingletonData.getInstance().sendToConsole(0xffffff, m_adviceThematic.name + " removido");
			Tweener.addTween(m_adviceAutoSave, { alpha: 1, time: 2 } );
			Tweener.addTween(m_adviceAutoSave, { alpha: 0, time: 2, delay: 5, onComplete:removeSprAutoSave } );
		}
		
		private function removeSprAutoSave():void 
		{
			m_stageReference.removeChild(m_adviceAutoSave);
			SingletonData.getInstance().sendToConsole(0xffffff, m_adviceAutoSave.name + " removido");
			dispatchEvent(new Event("ADVICES_FINISHED"));
		}
		
	}

}